package com.fingard.dsp.bank.directbank.gzelink01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IDeContract;
import com.fingard.dsp.bank.reqrespobj.contract.ContractState;
import com.fingard.dsp.bank.reqrespobj.decontract.ReqDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.RetAtsDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.detail.RetAtsDeContractDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;

public class GZELINK01DeContract extends GZELINK01Base implements IDeContract {
	@Override
	public RetAtsDeContract deContract(ReqDeContract p_req) throws Exception {
		RetAtsDeContract retAts = new RetAtsDeContract(p_req.reqHeader);
		RetAtsDeContractDetail tmpRetDetail = new RetAtsDeContractDetail();
		tmpRetDetail.reqSeqID = p_req.contractDetail.askNumber;

		try {
			ActSetItem actItem = getBankConfig().getActSetItem(p_req.contractDetail.recAct, "contract");
			if (actItem != null) {
				GZELINK01Header bankHeader = new GZELINK01Header();
				bankHeader.transCode = "311003";
				bankHeader.reqSeqNo = p_req.contractDetail.askNumber;
				
				StringBuilder reqMsg = new StringBuilder();
				reqMsg = writeCommonHeader(reqMsg, actItem, bankHeader);
				reqMsg.append("<SUBMIT_TIME>" + Format.DateTime14Format.format(new Date()) + "</SUBMIT_TIME>");
				reqMsg.append("<MERCHANT_ID>" + actItem.merchantId + "</MERCHANT_ID>");
				reqMsg.append("<PROTOCOL_NO>" + p_req.contractDetail.authCode + "</PROTOCOL_NO>");
				reqMsg.append("</TRANS_SUM>");
				reqMsg.append("</BODY>");
				reqMsg.append("</GZELINK>");
				
				String signData = reqMsg.toString().replaceAll("<SIGNED_MSG></SIGNED_MSG>", "");
				WriteBankLogLn2("签名原始数据:\r\n" + signData);
				
				String reqData = reqMsg.toString().replaceAll("<SIGNED_MSG></SIGNED_MSG>", "<SIGNED_MSG>" + signMsg(signData, actItem) + "</SIGNED_MSG>");
				WriteBankLogStep2(reqData);
				
				String[] tmpRetStr = sendDataToGZE(reqData, bankFront.getUrl("contractSubmit"));
				
				if(tmpRetStr[0].length() == 0) {
					WriteBankLogStep3(tmpRetStr[1]);
					
					StringBuilder tmpSbMsg = new StringBuilder();
					XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetStr[1], tmpSbMsg);
					if (tmpBkReader != null) {
						tmpRetDetail.respCode = "0";
						tmpRetDetail.respInfo = tmpBkReader.getFirstText("/GZELINK/INFO/ERR_MSG");
						
						String tmpRespCode = tmpBkReader.getFirstText("/GZELINK/INFO/RET_CODE");
						if ("0000".equals(tmpRespCode)) {
							String transStatus = tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/TRANS_STATUS");
							if ("0000".equals(transStatus) || "3073".equals(transStatus)) {
								tmpRetDetail.deSignState = ContractState.SUCCESS;
							} else {
								tmpRetDetail.deSignState = ContractState.FAIL;
							}
							String transInfo = tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/TRANS_INFO");;
							if (StringHelper.hasAnyChar(transInfo)) {
								tmpRetDetail.respInfo = transInfo;
							}
						} else {
							tmpRetDetail.respCode = "-1";
							tmpRetDetail.respInfo = tmpBkReader.getFirstText("/GZELINK/INFO/ERR_MSG");
						}
					} else {
						tmpRetDetail.respCode = "-1";
						tmpRetDetail.respInfo = tmpSbMsg.toString();
					}
				} else {
					tmpRetDetail.respCode = "-1"; 
					tmpRetDetail.respInfo = tmpRetStr[1];
				}
			} else {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = "未找到账号[" + p_req.contractDetail.recAct + "]商户配置信息!";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	@Override
	public RetAtsDeContract qryDeContract(ReqDeContract p_reqQry) throws Exception {
		RetAtsDeContract retAts = new RetAtsDeContract(p_reqQry.reqHeader);
		RetAtsDeContractDetail tmpRetDetail = new RetAtsDeContractDetail();
		tmpRetDetail.reqSeqID = p_reqQry.contractDetail.askNumber;
		
		try {
			ActSetItem actItem = getBankConfig().getActSetItem(p_reqQry.contractDetail.recAct, "contract");
			if (actItem != null) {
				GZELINK01Header bankHeader = new GZELINK01Header();
				bankHeader.transCode = "311004";
				bankHeader.reqSeqNo = p_reqQry.reqHeader.transSeqID;
				
				StringBuilder reqMsg = new StringBuilder();
				reqMsg = writeCommonHeader(reqMsg, actItem, bankHeader);
				
				reqMsg.append("<SUBMIT_TIME>" + Format.DateTime14Format.format(new Date()) + "</SUBMIT_TIME>");
				reqMsg.append("<MERCHANT_ID>" + actItem.merchantId + "</MERCHANT_ID>");
				reqMsg.append("<QUERY_MODE>4</QUERY_MODE>");
				reqMsg.append("<PROTOCOL_REQ_SN></PROTOCOL_REQ_SN>");
				reqMsg.append("<PROTOCOL_NO></PROTOCOL_NO>");
				reqMsg.append("<ACCOUNT_NO>" + p_reqQry.contractDetail.payAct + "</ACCOUNT_NO>");
				reqMsg.append("</TRANS_SUM>");
				reqMsg.append("</BODY>");
				reqMsg.append("</GZELINK>");
				
				String signData = reqMsg.toString().replaceAll("<SIGNED_MSG></SIGNED_MSG>", "");
				WriteBankLogLn2("待签名数据:\r\n" + signData);
				
				String reqData = reqMsg.toString().replaceAll("<SIGNED_MSG></SIGNED_MSG>", "<SIGNED_MSG>" + signMsg(signData, actItem) + "</SIGNED_MSG>");
				WriteBankLogStep2(reqData);
				
				String[] tmpRetStr = sendDataToGZE(reqData, bankFront.getUrl("contractSubmit"));
				if(tmpRetStr[0].length() == 0) {
					WriteBankLogStep3(tmpRetStr[1]);
					
					StringBuilder tmpSbMsg = new StringBuilder();
					XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetStr[1], tmpSbMsg);
					
					if (tmpBkReader != null) {
						tmpRetDetail.respCode = "0";
						tmpRetDetail.respInfo = tmpBkReader.getFirstText("/GZELINK/INFO/ERR_MSG");
						
						String tmpRespCode = tmpBkReader.getFirstText("/GZELINK/INFO/RET_CODE");
						if ("0000".equals(tmpRespCode)) {
							String tmpRespInfo = tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/TRANS_INFO");
							if (StringHelper.hasAnyChar(tmpRespInfo)) {
								tmpRetDetail.respInfo = tmpRespInfo;
							}
							
							// 0-已签约、1-已解约(解约后数据仍然存在，会一直返回已解约)、2-未签约
							String contractState = tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/SIGN_STATUS");
							if ("1".equals(contractState)) {
								tmpRetDetail.deSignState = ContractState.SUCCESS;
							} else {
								tmpRetDetail.deSignState = ContractState.FAIL;
							}
						} else {
							tmpRetDetail.respCode = "-1";
							tmpRetDetail.respInfo = tmpBkReader.getFirstText("/GZELINK/INFO/ERR_MSG");
						}
					} else {
						tmpRetDetail.respCode = "-1";
						tmpRetDetail.respInfo = tmpSbMsg.toString();
					}
				} else {
					tmpRetDetail.respCode = "-1"; 
					tmpRetDetail.respInfo = tmpRetStr[1];
				}
			} else {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = "未找到账号:[" + p_reqQry.contractDetail.recAct + "]商户配置信息!";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}
}
